﻿
Namespace PL.MESSDK
    Public Class Specification_PL
        Inherits PLBase
        Implements IPLProcess

        Public Property SequenceGeneration As Short = 1


        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
        End Sub
        Public Sub New()
            MyBase.New()
        End Sub

        ''' <summary>
        ''' Cette procedure est appellé pour chaque ligne qui sera traité.
        ''' quand le Me.MetaDataTable.Adapter.Update() est appelé via ApplyChanges()
        ''' Si on veut faire de la logique sur les lignes il faut vérifier le 
        ''' DataRowState.Added, DataRowState.Modified etc...
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Overridable Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating
            If e.Command.CommandText = "bsp_Specification_PL_Save" Then
                SequenceGeneration = AppOption.SpecificationGenerationSequenceCode
                'Pour le Save sur un ADD on assign la sequence
                Me.MetaDataTable.Adapter.InsertCommand.Parameters("@GenerateSEQ").Value = SequenceGeneration
                Me.MetaDataTable.Adapter.UpdateCommand.Parameters("@GenerateSEQ").Value = SequenceGeneration

            End If
        End Sub

        Public Overrides Function Delete() As Boolean
            'Throw New NotImplementedException("Delete To Come...")
            Return False
        End Function

        Public Overrides Function Validate() As Boolean
            'Throw New NotImplementedException("Validate To Come...")
            Return True
        End Function

        Public Overrides Sub ValidateRow(row As DataRow)
            row.ClearErrors()
            If AppOption.SpecificationGenerationSequenceCode = 0 Then
                If row.Table.Columns.Contains("SpecCd") Then
                    If IsDBNull(row("SpecCd")) Then
                        row.SetColumnError("SpecCd", "Mandatory")
                    End If
                End If
            End If
            'Test point : PRM for ALT, ALT for PRM
            If row.Table.Columns.Contains("SpecDescr_PRM") Then
                If IsDBNull(row("SpecDescr_PRM")) Then

                    If row.Table.Columns.Contains("SpecDescr_ALT") Then
                        If IsDBNull(row("SpecDescr_ALT")) Then
                            row.SetColumnError("SpecDescr_ALT", "Mandatory")
                            row.SetColumnError("SpecDescr_PRM", "Mandatory")
                        Else
                            row("SpecDescr_PRM") = row("SpecDescr_ALT")
                        End If
                    End If
                End If
            End If

            If row.Table.Columns.Contains("SpecDescr_ALT") Then
                If IsDBNull(row("SpecDescr_ALT")) Then
                    If row.Table.Columns.Contains("SpecDescr_PRM") Then
                        If IsDBNull(row("SpecDescr_PRM")) Then
                            row.SetColumnError("SpecDescr_PRM", "Mandatory")
                            row.SetColumnError("SpecDescr_ALT", "Mandatory")
                        Else
                            row("SpecDescr_ALT") = row("SpecDescr_PRM")
                        End If
                    End If
                End If
            End If

            If row.Table.Columns.Contains("ResultValidationID_FK") Then
                If IsDBNull(row("ResultValidationID_FK")) Then
                    row.SetColumnError("ResultValidationID_FK", "Mandatory")
                End If
            End If

            If row.HasErrors Then
                row.RowError = "Please check your columns, a validation error occur"
            End If
        End Sub

        Public Shared Function PublishSpec(ByVal specID As Guid?, ByVal specCd As String, Optional ByVal queryname As String = "") As Boolean
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            If Not String.IsNullOrEmpty(queryname) Then
                Try
                    If specID IsNot Nothing AndAlso Not String.IsNullOrEmpty(specCd) Then
                        sqlcmd = MetaQuery.GetSPCmdByMetaName(queryname)
                        sqlcmd.Parameters("@SpecificationCode").Value = specCd
                        sqlcmd.Parameters("@Specification").Value = specID

                        sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)
                    End If
                Catch sqlex As System.Data.SqlClient.SqlException
                    Throw sqlex
                Finally
                    If sqlcmd IsNot Nothing Then
                        sqlcmd.Dispose()
                    End If
                End Try

                Return True
            Else
                Return False
            End If

        End Function

        Public Function BatchRecords(dataRow() As DataRow) As Boolean Implements IPLProcess.BatchRecords
            Return True
        End Function

        Public Function CurrentRecord(dataRow As DataRow) As Boolean Implements IPLProcess.CurrentRecord
            Return True
        End Function

        Public Overridable Function NewRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.NewRecord
            'Si on arrive d'un TreeList...
            If Me.ID IsNot Nothing Then
                dataRow("SpecID") = ID
            Else
                dataRow("SpecID") = Guid.NewGuid
            End If
            dataRow("Inactive") = 0
            dataRow("SpecList") = 0
            dataRow("System") = 0
            Return True

        End Function
    End Class
End Namespace
